package com.bootdo.wx.api;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.wx.domain.OrderOrderDO;
import com.bootdo.wx.domain.OrderShippingDO;
import com.bootdo.wx.domain.ShopExpressMessageDO;
import com.bootdo.wx.service.OrderOrderService;
import com.bootdo.wx.service.OrderShippingService;
import com.bootdo.wx.service.ShopExpressMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by HP on 2019/5/29.
 */
@Controller
@RequestMapping("/api/shopExpress")
public class ApiShopExpressController {

    @Autowired
    private OrderOrderService orderOrderService;
    @Autowired
    private ShopExpressMessageService shopExpressMessageService;
    @Autowired
    private OrderShippingService orderShippingService;

    /**
     * 根据订单id查询物流信息
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/express/message")
    public R expressMessage(@RequestBody Map<String, Object> params){
        if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                || !params.containsKey("orderId") || StringUtils.isBlank(params.get("orderId").toString())
                ){
            return R.error("参数错误");
        }
        try{
            String orderId = params.get("orderId").toString();
            OrderOrderDO orderDO = orderOrderService.get(orderId);
            if (orderDO==null){
                return R.error("订单信息异常");
            }
            if (orderDO.getStatus()!=4){
                return R.error("订单状态异常");
            }
            ShopExpressMessageDO messageDO = shopExpressMessageService.queryByOrderId(orderId);
            OrderShippingDO orderShippingDO = orderShippingService.list(params).get(0);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("messageDO",messageDO);
            map.put("orderShippingDO",orderShippingDO);
            return R.ok().put("data", map);
        }catch (Exception e){
            return R.error("查询订单物流失败");
        }
    }
}
